/**
 * Project Name:template-web
 * File Name   :SessionInterceptor.java
 * Package Name:org.cn.template.component.web.interceptor
 * Date:2015年9月27日下午5:41:23
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package org.cn.template.component.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.MDC;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * ClassName:SessionInterceptor <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason	:TODO ADD REASON. <br/>
 * Date     :2015年9月27日 下午5:41:23 <br/>
 * @author  :http://my.oschina.net/httpssl
 * @email   :491835898@QQ.COM
 * @since   :JDK 1.7
 * @see 	 
 */
public class SessionInterceptor extends HandlerInterceptorAdapter
{
	/**
	 * 会话ID
	 */
	private final static String SESSION_KEY = "sessionId";

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
			Exception arg3) throws Exception
	{
		// 删除
		MDC.remove(SESSION_KEY);
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
			ModelAndView arg3) throws Exception
	{
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception
	{
		// 放SessionId
		String token = java.util.UUID.randomUUID().toString();
		MDC.put(SESSION_KEY, token);
		return true;
	}

}